/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import Utils.Sell;
import Utils.User;
import Utils.dbManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alice
 */
@WebServlet(name="BuyerLandingServlet", urlPatterns={"/BuyerLandingServlet"})
public class BuyerLandingServlet extends HttpServlet {
     private dbManager manager;
   
    @Override
public void init() throws ServletException {
// inizializza il DBManager dagli attributi di Application
this.manager = (dbManager)super.getServletContext().getAttribute("dbmanager");
}

    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            PrintWriter out = response.getWriter();
            HttpSession session = request.getSession(false);
            if(session.getAttribute("buyer")==null)
                response.sendRedirect(request.getContextPath() + "/ErrorLoginServlet");
                
            String mex=(String)session.getAttribute("confirm2");
        
            
            User user=(User) session.getAttribute("buyer");
            int idu= user.getId_User();
            String usernameb = user.getUsername();
        
           List <Sell> sell=manager.urlPDF(idu);
            
            response.setContentType("text/html");
        
            String docType=
             "<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">\n";    
    out.println(docType+"<html>");
    out.println("<head>"
    + "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/bootstrap.css\"> ");
    out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
    out.println("<title>LANDINGBUYER Page</title>");
    out.println("<meta name=\"generator\" content=\"WYSIWYG Web Builder 8 - http://www.wysiwygwebbuilder.com\">");
    out.println("<style type=\"text/css\">");
    out.println("</style>");
    out.println("</head>");
    out.println("<body>");
    out.println("<div class=\"navbar  navbar-inverse nvbar-fixed-top\">" +
    "<div class=\"navbar-inner\">" +
    "<div class=\"container\">" +
     

    "<a class=\"btn btn-navbar navbar-inverse nvbar-fixed-top \" data-toggle=\"collapse\" data-target=\".nav-collapse\">" +
    "<span class=\"icon-bar\"></span>" +
    "<span class=\"icon-bar\"></span>" +
    "<span class=\"icon-bar\"></span>" +
    "</a>" +
     


               " <ul class=\"nav\">" +
       " <li><a href=\"#\"></a></li>" +
       " <li class=\"active\">" +
       " <a href=\"#\">Buyer Page</a>" +
       " </li>" +     
       " <li><a href=\"http://localhost:8084/PROGETTO_WEB_1/LogoutServlet\">Log Out</a></li>" +
       " </ul>" +

    "<div class=\"nav-collapse collapse\">" +

    "</div>" +
     
    "</div>" +
    "</div> </div>");

    out.println("<div id=\"wb_Text1\" style=\"position:absolute;left:84px;top:64px;\"> ");
    out.println("<h2>Buyer Page</h2> </div>");
     if (mex != null){ out.println("<div style=\"position:absolute;left:84px;top:124px;\"><h4 class=\"text-error\">"+mex+"</h4></div>");
    }  
      
   
    
     
    out.println("<div  style=\"position:absolute;left:86px;top:300px;width:154px;height:27px;z-index:1;\" >" +
    "<form method=\"get\" action=\"BuyerProdottiSearch\">" +
    "<select name=\"combobox\" >" +
    "<option>Ortaggi</option>" +
    "<option>Formaggi</option>" +
    "<option>Frutta</option>" +
    "<option>Agrumi</option>" +
    "</select> " +
    "<input type=\"submit\" onclick=a href=\"http://localhost:8084/PROGETTO_WEB_1/BuyerProdottiSearch\" value=\"Cerca\" class=\"btn btn-success\"  >" +
    "</form> </div>");
    out.println("<div id=\"wb_Text2\" style=\"position:absolute;left:84px;top:216px;\">");
    out.println("<h4> Categoria dei Prodotti in vendita</h4> </div>");
    out.println("<div id=\"wb_Text3\" style=\"position:absolute;left:84px;top:265px;\">");
    out.println("<h6>Selezionare la categoria del prodotto cercato</h6> </div>");
 
     
    out.println("<table id=\"selectable\" class=\"table table-bordered table-hover\" style=\"position:absolute;left:84px;top:400px;width:400px;\">\n"+
    "        <tr>\n" +        
    "            <th>\n" +
    "                PDF\n" +
    "            </th>   \n" +
    "         </tr>");        
    for (int i = 0; i < sell.size(); i++) {
        String p= sell.get(i).getUrl();
       
   
        out.println("<tr><td><a  href=\""+request.getContextPath()+"/PDF/"+usernameb+"/"+p+"\"><p class=\"text-info\">"+p+"</p></td></tr>");
      }  
    
    out.println("</table>");    
    out.println("</body>");
    out.println("</html>");
    session.setAttribute("confirm2", null);
     
        } catch (SQLException ex) {
            Logger.getLogger(BuyerLandingServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
      
    
    
    }

    
}
